package java;

public class Question35 {
    public static void main(String[] args) {
        int[] nums = {1,3,5,6 ,7};
        System.out.println(searchInsert(nums,5));
    }

    public static int searchInsert(int[] nums, int target) {
        if (nums == null || nums.length < 1) {
            return -1;
        }
        int left = 0;
        int right = nums.length - 1;
        while (left < right) {
            int center = (left + right)/2;
            if (target < nums[center]){
                right = center - 1;
            }else if (target > nums[center]){
                left = center + 1;
            }else {
                return center;
            }
        }
        if (target <= nums[left]){
            return left;
        }else {
            return left + 1;
        }
    }
}
